<?php

namespace app\xwam\controller;

/**
 * Soft 控制器类
 *
 * @version 1.0 2018-1-31 SoChishun(14507247@qq.com) Added.
 */
class SoftController extends AppbaseController {

    function indexSearch() {
        $search = input('param.');
        $where = array();
        if (!empty($search['word'])) {
            $where['a.title'] = array('like', '%' . $search['word'] . '%');
        }
        if (!empty($search['category_id'])) {
            $where['a.category_id'] = $search['category_id'];
        }
        if (!empty($search['title'])) {
            $where['a.title'] = array('like', '%' . $search['title'] . '%');
        }
        // $where['site_id'] = $this->site_id;
        return array('search' => $search, 'where' => $where);
    }

    function index() {
        $asearch = $this->indexSearch();
        $list = db('content_soft')->alias('a')->join('t_content_soft_category c', 'a.category_id=c.id', 'left')->field('a.*, c.title as category_title')->where($asearch['where'])->order('a.id desc')->paginate();
        $this->assign('search', $asearch['search']);
        $this->assign('list', $list);
        $this->assign('page', $list->render());
        return view();
    }

    function edit($id = 0, $category_id = 0) {
        $data = array();
        if ($id) {
            $data = db('content_soft')->find($id);
        }
        if (!$data) {
            $data = array('category_id' => $category_id, 'user_name' => $this->userName);
        }
        $this->assign('data', $data);
        return view();
    }

    function editSave() {
        $mSoft = new \app\xwam\model\SoftModel();
        $result = $mSoft->saveSoft();
        $this->dialogJump($result['status'], $result['info']);
    }

    function changeStatus($id, $status) {
        $mSoft = new \app\xwam\model\SoftModel();
        return json_encode($mSoft->changeStatus($id, $status));
    }

    function changeSort($id, $sort) {
        $mSoft = new \app\xwam\model\SoftModel();
        return json_encode($mSoft->changeSort($id, $sort));
    }

    function delete($id = '') {
        $mSoft = new \app\xwam\model\SoftModel();
        $result = $mSoft->deleteSoft($id);
        return json_encode($result);
    }

    // 2017-3-20
    function category($id = '') {
        if (!$id) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $mSoft = new \app\xwam\model\SoftModel();
        $list = $mSoft->where(array('id' => array('in', $id)))->field('id, title, category_id')->order('sort, id desc')->select();
        $this->assign('list', $list);
        return view();
    }

    /**
     * 保存类别
     * @version 1.0 2016-3-20 SoChishun Added.
     */
    public function categorySave() {
        $aid = input('id/a');
        $aCategoryId = input('category_id/a');
        $mSoft = new \app\xwam\model\SoftModel();
        $result = $mSoft->saveCategory($aid, $aCategoryId);
        if (!$result['status']) {
            $this->dialogClose(false, array('error' => '参数有误!'));
        }
        $this->dialogJump(true);
    }

}
